--
-- Author: shunguo.chen
-- Date: 2017-09-11 11:56:12
--
-- ZoneRender	服务器信息

local ClassRef = app.mvc.createRenderClass(...)

local secondsOfDay = 24 * 60 * 60
local LOGIN_DATE_STR = L("lua_code_text_117")


-- 1：新开，2：火爆，3：畅通，4：维护 5：推荐
local ServerStatusConfig = {
	["1"] = {name = L("ServerStatus_NewOpen"), color = cc.c3b(118, 255, 134) },
	["2"] = {name = L("ServerStatus_Hot"), color = cc.c3b(255, 144, 0) },
	["3"] = {name = L("ServerStatus_Unimpeded"), color = display.COLOR_GREEN },
	["4"] = {name = L("ServerStatus_Maintain"), color = cc.c3b(179, 179, 179) },
	["5"] = {name = L("ServerStatus_Recommend"), color = cc.c3b(84, 201, 255) },
}
-- 把一个时间转化成日期，tarTime<=curTime
-- tarTime      目标时间
-- curTime      当前时间
local function timeToDateStr(tarTime, curTime)
    local curDate = os.date("*t", curTime)
    curDate.hour = 0
    curDate.min = 0
    curDate.sec = 0

    local todayStart = os.time(curDate)
    local dateStr = ""
    if tarTime > todayStart then
        -- 是今天
        dateStr = L("lua_code_text_266")
    elseif tarTime <= todayStart and tarTime >= (todayStart - secondsOfDay) then
        -- 是昨天
        dateStr = L("lua_code_text_448")
    else
    	dateStr = os.date("%Y.%m.%d", tarTime)
    end
    return dateStr
end

--data = ZoneData
function ClassRef.create(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.LoginZoneNode2))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self:setContentSize(self._ui.Panel:getContentSize())

	-- btns
	self._ui.Button_bg:setPressedActionEnabled(false)
	self._ui.Button_bg:addClickEventListener(function()
		if self._data then
			executeFunc(self._onBtnClick, self)
		end
	end)

	self:setSelected(false)

    -- TODO 位置应该通过UI调整
	self._ui.tips:setPositionX(self._ui.name:getPositionX() + 10)
	self._ui.tips:setPositionY(self._ui.name:getPositionY())
	self._ui.tips:setVisible(true)
	self._ui.tips:enableOutline(cc.c4b(57,31,21,255), 2)

	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		self._ui.name:setString(self._data.name)

		if self._data.lastVisitTS then
			local dateStr = timeToDateStr(self._data.lastVisitTS, app:getTime())
			self._ui.tlt:setString(string.format(LOGIN_DATE_STR, dateStr))
		else
			self._ui.tlt:setString("")
		end

		local statusConfig
		if self._data.status then
			statusConfig = ServerStatusConfig[self._data.status]
		end
		if not statusConfig then
			statusConfig = {name = "", color = display.COLOR_WHITE}
		end
		self._ui.tips:setString(statusConfig.name)
		self._ui.tips:setTextColor(statusConfig.color)
		local posx = self._ui.name:getPositionX() + self._ui.name:getContentSize().width + 5
		self._ui.tips:setPositionX(posx)
	end
end

function ClassRef:onBtnClick(cb)
	self._onBtnClick = cb
end

function ClassRef:isSelected()
	return not self._ui.Button_bg:isBright()
end
function ClassRef:setSelected(b)
	self._ui.Button_bg:setBright(not b)
end

return ClassRef